package com.handinfo.uimenu.window;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.handinfo.game.ActorItem;
import com.handinfo.game.ActorRole;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UITools;
import com.handinfo.util.ArrayList;

/**
 * 处理背包的类（标签，背包道具格子）
 * @author venizeng
 * 
 */
public class ActorItemLBHandler extends AbsLBHandler
{

	protected static final int NUM_LABEL_DIS = 5;

	//
	public ActorItemLBHandler(ILBHandler iHandler)
	{
		super(iHandler);
		m_num_label = NUM_LABEL_DIS;
		// m_labels = new String[] { "全部", "装备", "消耗", "材料", "任务", "其他" };
		m_labels = new Image[] { UIManager.IMAGE_LABEL_QUANBU, UIManager.IMAGE_LABEL_ZHUANGBEI,
				UIManager.IMAGE_LABEL_XIAOHAO, UIManager.IMAGE_LABEL_CAILIAO, UIManager.IMAGE_LABEL_QITA,
				UIManager.IMAGE_LABEL_QITA };
		//
		m_w_label = UITools.W_LABEL_MIDDLE + 8;
		m_iw_label = 1;
	}

	protected int getLabelIdByLabelIndex(int labelGroupIndex)
	{
		return labelGroupIndex + ActorItem.TYPE_GOODS_ALL;// 标签映射规则 ;
	}

	protected int getLabelIndexByLabelId(int labelId)
	{
		return labelId + ActorItem.TYPE_GOODS_ALL;
	}

	/**
	 * 过滤规则
	 */
	public Object[] filteDatas(Object datas)
	{
		ActorItem[] items = (ActorItem[]) datas;
		if (items == null) {
			return null;
		}
		if (m_id_label == ActorItem.TYPE_GOODS_ALL) {
			return items;
		}
		ArrayList list = new ArrayList();
		for (int i = 0, len = items.length; i < len; i++) {
			if (items[i] == null) {
				continue;
			}
			switch (m_id_label) {
				case ActorItem.TYPE_GOODS_TASK:
				case ActorItem.TYPE_GOODS_OTHER:
					if (items[i].m_type == ActorItem.TYPE_GOODS_TASK || items[i].m_type == ActorItem.TYPE_GOODS_OTHER) {
						list.addElement(items[i]);
					}
					break;
				default:
					if (items[i].m_type == m_id_label) {
						list.addElement(items[i]);
					}
					break;
			}
		}
		int size = list.size();
		if (size == 0) {
			return null;
		}
		ActorItem[] results = new ActorItem[size];
		for (int i = 0; i < size; i++) {
			results[i] = (ActorItem) list.elementAt(i);
		}
		return results;
	}

	/**
	 * 绘制背包
	 * @param g
	 * @param x
	 * @param y
	 */
	public void paintBag(Graphics g)
	{
		paintActorItem(g);
	}

	/**
	 * 获得最下一个未锁的格子
	 */
	public int getLastUnLockItemIndex()
	{
		int index = -1;
		int NUM_BAG_DIS = m_row * NUM_BAG_COL ;
		for (int i = NUM_BAG_DIS - 1; i >= 0; i--) {
			index = i;
			if (i + m_index_data_top < ActorRole.s_num_open) {
				break;
			}
		}
		return index + INDEX_BAG_FIRST;
	}

	protected int keyDown()
	{
		return keyBagDown();
	}

	protected int keyRight()
	{
		return keyBagRight();
	}
}
